package com.hyfrogx.modules.zoom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.validator.ValidatorUtils;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.sys.controller.AbstractController;
import com.hyfrogx.modules.zoom.entity.HyNmtPower;
import com.hyfrogx.modules.zoom.form.NmtPowerForm;
import com.hyfrogx.modules.zoom.service.HyNmtPowerService;
import com.hyfrogx.modules.zoom.service.HyPowerFbService;
import com.hyfrogx.modules.zoom.service.HyPowerOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;

/**
 * @author ：Li9527
 * @date ：Created in 2019-11-18 19:11
 * @description：${description}
 * @modified By：
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/nmtPower")
@Api(value = "会议特权管理controller", tags = {"后台--会议特权管理"})
public class HyNmtPowerController extends AbstractController {

    @Autowired
    HyNmtPowerService hyNmtPowerService;

    @Autowired
    HyPowerOrderService powerOrderService;

    @Autowired
    HyPowerFbService hyPowerFbService;

    /**
     * 获取所有特权记录,按优先使用中然后更新时间倒序
     *
     * @param params
     * @return list
     */
    @GetMapping("/list")
    @ApiOperation("获取所有特权记录,按优先使用中然后更新时间倒序")
    public R getVipZoomlist(@RequestParam Map<String, Object> params) {
        Page page = hyNmtPowerService.selectPageByConditions(new QueryExt(params));
        return R.ok().put("page", page);
    }

    /**
     * 创建特权卡
     *
     * @param
     * @return Integer
     */
    @PostMapping("/create")
    @ApiOperation("创建特权卡")
    public R createNmtPower(@RequestBody NmtPowerForm powerForm) {
        ValidatorUtils.validateClientEntity(powerForm);
        HyNmtPower hyNmtPower = new HyNmtPower();
        BeanUtils.copyProperties(powerForm, hyNmtPower);
        hyNmtPower.setStatus(FrogConstant.Status.FLASE);
        hyNmtPower.setCratetime(new Date());
        hyNmtPower.setCuid(getUserId());
        Boolean flag = hyNmtPowerService.saveOrUpdate(hyNmtPower);
        if(flag){
            return R.ok("新增成功");
        }else{
            return R.error("新增失败");
        }
    }


    /**
     * 编辑特权卡(包含启用/禁用)
     *
     * @param powerForm
     * @return Integer
     */
    @PostMapping("/edit")
    @ApiOperation("编辑特权卡(包含启用/禁用)")
    public R editNmtPower(@RequestBody NmtPowerForm powerForm) {
        if (powerForm.getId() == null) {
            return R.error("编辑特权卡信息时,特权卡主键id必传");
        }
        ValidatorUtils.validateClientEntity(powerForm);
        HyNmtPower hyNmtPower = new HyNmtPower();
        BeanUtils.copyProperties(powerForm, hyNmtPower);
        hyNmtPower.setUpdatetime(new Date());
        hyNmtPower.setCuid(getUserId());
        Boolean flag = hyNmtPowerService.saveOrUpdate(hyNmtPower);
        if(flag){
            return R.ok("编辑成功");
        }else{
            return R.error("编辑失败");
        }
    }

    /**
     * 分页获取我的购买记录
     *
     * @param params
     * @return list
     */
    @GetMapping("/getPowerOrderList")
    @ApiOperation("获取所有特权记录,按优先使用中然后更新时间倒序")
    public R getPowerOrderList(@RequestParam Map<String, Object> params) {
        try {
            Page page = powerOrderService.selectPageByConditions(new QueryExt(params));
            if (page.getRecords() != null) {
                return R.ok().put("page", page);
            } else {
                return R.error("获取失败");
            }
        } catch (MyException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 分页查询订单反馈
     *
     * @param params
     * @return
     */
    @GetMapping("getFeedbackList")
    @ApiOperation("分页查询订单反馈")
    public R getFeedbackList(@RequestParam Map<String, Object> params) {
        try {
            Page page = hyPowerFbService.selectPageByConditions(new QueryExt(params));
            if (page.getRecords() != null) {
                return R.ok().put("page", page);
            } else {
                return R.error("获取失败");
            }
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }


}
